package org.neowei.image.commond.Handle;

import org.neowei.image.api.ImageOption;
import org.neowei.image.commond.CmdOption;

public class CropResizeHandle extends AbstractHandle {

	@Override
	public void doHandle(ImageOption imageOption) throws Exception {
		CmdOption cmdOption = this.getCmdOption("convert");
		imageOption.setSrcWidthHeight();
		String crop;
		if (imageOption.getNeowidth() < imageOption.getNeoheight()) {
			int rwidth = imageOption.getNeoheight() * imageOption.getSrcwidth() / imageOption.getSrcheight();
			cmdOption.AddRow("-resize", super.getStrHeightWidth(rwidth, imageOption.getNeoheight()));
			StringBuilder sb = new StringBuilder(super.getStrHeightWidth(imageOption.getNeowidth(), imageOption.getNeoheight()));
			crop = sb.append('+').append(String.valueOf((rwidth - imageOption.getNeowidth()) / 2)).append("+0").toString();
		} else {
			int rheight = imageOption.getNeowidth() * imageOption.getSrcheight() / imageOption.getSrcwidth();
			cmdOption.AddRow("-resize", super.getStrHeightWidth(imageOption.getNeowidth(), rheight));
			StringBuilder sb = new StringBuilder(super.getStrHeightWidth(imageOption.getNeowidth(), imageOption.getNeoheight()));
			crop = sb.append("+0").append('+').append(String.valueOf((rheight - imageOption.getNeoheight()) / 2)).toString();
		}
		cmdOption.AddRow("-crop", crop);
		super.crop(cmdOption);
	}
}
